package com.woniuxy.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.entity.Shopinfo;
import com.woniuxy.mapper.ShopinfoMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by xxhcm on 2021/3/24.
 */
public class DbRealm  extends AuthorizingRealm {
    @Autowired
    private ShopinfoMapper shopinfoMapper;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        return null;
        String account =  (String) token.getPrincipal();
        QueryWrapper<Shopinfo>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        Shopinfo shopinfo = shopinfoMapper.selectOne(queryWrapper);
        if(shopinfo==null){
            throw new UnknownAccountException("无此账号");
        }
        return new SimpleAuthenticationInfo(shopinfo.getAccount(), shopinfo.getPassword(),this.getName());
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return super.supports(token);
    }
}
